package com.example.newsapp.ui.components.otherComponent.searchResultComponent

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.`object`.PageNavigation
import com.example.newsapp.data.`object`.StateSave
import kotlinx.coroutines.runBlocking

// 搜索结果页面顶部搜索框
@Composable
fun ResultTopBar(focusRequester: FocusRequester, modifier: Modifier = Modifier) {
    val navController = LocalNavController.current
    val keyboardController = LocalSoftwareKeyboardController.current

    // 顶部搜索框
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(start = 5.dp, top = 5.dp, bottom = 10.dp),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 返回按钮
        Icon(
            imageVector = Icons.Default.ArrowBack,
            contentDescription = null,
            modifier = Modifier
                .clickable(
                    interactionSource = null,
                    indication = null
                ) {
                    StateSave.hotInitState++
                    runBlocking {
                        keyboardController?.hide()
                    }
                    navController.popBackStack()
                }
        )
        Spacer(modifier = Modifier.width(10.dp))
        var value by rememberSaveable { mutableStateOf("") }
        // 搜索框
        BasicTextField(
            value = value,
            onValueChange = { value = it },
            modifier = Modifier
                .weight(1f)
                .height(35.dp)
                .border(1.dp, Color.Gray, RoundedCornerShape(50))
                .clip(RoundedCornerShape(50))
                .focusRequester(focusRequester)
                .background(Color.White),
            textStyle = TextStyle(fontSize = 19.sp),
            singleLine = true,
            keyboardOptions = KeyboardOptions(
                imeAction = ImeAction.Search
            ),
            keyboardActions = KeyboardActions(
                onSearch = {
                    keyboardController?.hide()
                    navController.navigate("${PageNavigation.searchResult}?title=${value}")
                }
            ),
            decorationBox = {
                Row(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(start = 10.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Icon(
                        imageVector = Icons.Default.Search,
                        contentDescription = null,
                        tint = Color(0xFF656565)
                    )
                    Spacer(modifier = Modifier.width(5.dp))
                    it()
                }
            }
        )
        // 搜素按钮
        TextButton (
            onClick = {
                StateSave.hotInitState++
                focusRequester.freeFocus()
                keyboardController?.hide()
                navController.navigate("${PageNavigation.searchResult}?title=${value}")
            }
        ) {
            Text(text = "搜索", fontSize = 18.sp)
        }
    }
}